<!--案件处理，承办中心-->
<template>
  <div>
    <tablePage ref="tablePage" canFold :searchForm.sync="searchForm" :searchFormRules="searchFormRules" :requestFunc="requestFunc">
      <template slot="search-form">
        <searchFormItemsCommon type="process" hideProcessingDepartment :statusArr="statusArr" :form.sync="searchForm" :rules.sync="searchFormRules"></searchFormItemsCommon>
      </template>

      <template slot="rows">
        <tableColumnsCommon fixed="right" type="process" @showDetailsDialog="handleShowDialog('detailsDialog', $event)" @showLeaveMessageDialog="handleShowDialog('leaveMessageDialog', $event)"></tableColumnsCommon>
      </template>
    </tablePage>

    <detailsDialog ref="detailsDialog" type="process" :visible.sync="detailsDialog.visible" :id="currentRow.id" @read="handleReadComment">
      <template v-slot:footer-btn="{ details, workOrderPermission }">
        <!-- 材料订正状态 materialCorrectStatus -->
        <template v-if="details.auditFlag === 'N' && details.materialCorrectStatus !== 0">
          <!-- 回退中-->
          <template v-if="details.status === 1">
            <el-button v-permission="['允许取消回退']" @click="handleCancel" type="primary">取消回退</el-button>
          </template>
          <!-- 待确认-->
          <template v-if="details.status === 2">
            <el-button @click="handleStart" type="primary">确认接收</el-button>
            <!--
              同一单位同一案件只能申请复核一次 
              是否已申请过复核 0否 1是 
            -->
            <el-button v-if="details.hasRepeatCheck === 0 && workOrderPermission.repeatCheckPermission === '1'" @click="appealDialog.visible = true" type="primary" plain>申请复核</el-button>
            <!-- 复核不通过显示确认退回按钮 -->
            <!-- 
              1是被复核拒绝过 0 没被拒绝过 
             -->
            <el-button v-if="details.hasRejectRepeatCheck === 1 && workOrderPermission.returnPermission === '1'" @click="confirmBack.visible = true" type="primary" plain>确认退回</el-button>
          </template>
          <!-- 办理中、处理中-->
          <template v-if="details.status === 3 && details.subStatus !== 7">
            <el-button @click="onCompleteCase(details)" type="primary">提交办结</el-button>
            <el-button @click="overDialog.visible = true" type="primary" plain>提交终止</el-button>
            <el-button @click="suspendDialog.visible = true" type="primary" plain>中止</el-button>
            <el-button @click="onYanQi(details)" type="primary" plain>延期</el-button>
            <!-- <el-button v-permission="['允许取消确认']" @click="handleCancelStart" type="primary" plain>取消确认</el-button> -->
            <!-- <el-button @click="overDialog.visible= true" type="danger" plain>终止</el-button>-->
          </template>
          <!-- 待办结 -->
          <!-- <template v-if="details.status === 4">
            <el-button v-permission="['允许取消提交办结']" @click="handleCancelComplete" type="primary">取消提交办结</el-button>
          </template> -->
          <!-- 已中止 状态废弃-->
          <!-- <template v-if="details.status === 7"> -->
          <template v-if="details.status === 3 && details.subStatus === 7">
            <el-button @click="handleContinueProcess" type="primary">继续办理</el-button>
          </template>
        </template>
      </template>
    </detailsDialog>

    <!-- 留言 -->
    <leaveMessageDialog :visible.sync="leaveMessageDialog.visible" :workOrderNo="currentRow.workOrderNo" :id="currentRow.id" @read="handleReadLeaveMessage"></leaveMessageDialog>
    <!-- 申请复核 -->
    <appealDialog :visible.sync="appealDialog.visible" :workOrderNo="currentRow.workOrderNo" @success="rePlayLoadData"></appealDialog>
    <!-- 中止 -->
    <suspendDialog type="process" :visible.sync="suspendDialog.visible" :workOrderNo="currentRow.workOrderNo" @success="rePlayLoadData"></suspendDialog>
    <!-- 延期 -->
    <postponeDialog type="process" :visible.sync="postponeDialog.visible" :addTimeNum="currentRow.addTimeNum" :workOrderNo="currentRow.workOrderNo" @success="rePlayLoadData"></postponeDialog>
    <!-- 完成 -->
    <completeDialog :visible.sync="completeDialog.visible" :workOrderNo="currentRow.workOrderNo" :workOrderId="currentRow.id" :hasDebtAmount="hasDebtAmount" @success="rePlayLoadData"></completeDialog>

    <!-- 终止 -->
    <overDialog :visible.sync="overDialog.visible" :workOrderNo="currentRow.workOrderNo" @success="rePlayLoadData"></overDialog>
    <!-- 确认退回 -->
    <confirm-back :visible.sync="confirmBack.visible" :workOrderNo="currentRow.workOrderNo" @success="rePlayLoadData"></confirm-back>
  </div>
</template>

<script>
import tablePage from "@/components/tablePage/index.vue";
import searchFormItemsCommon from "@/views/workOrder/components/searchFormItemsCommon/searchFormItemsCommon";
import tableColumnsCommon from "@/views/workOrder/components/tableColumnsCommon";
import detailsDialog from "./components/detailsDialog";
import leaveMessageDialog from "./components/leaveMessageDialog";
import appealDialog from "./components/appealDialog";
import suspendDialog from "./components/suspendDialog";
import postponeDialog from "./components/postponeDialog";
import completeDialog from "./components/completeDialog";
import overDialog from "./components/overDialog";

import * as workOrderApi from "@/api/workOrder/index";
import { workOrderStatus, timeRemaining, workOrderPublicStatus } from "@/utils/filters";
import { defaultSearchFromForURLQueryMixin, initSearchForm, initSearchFormRules } from "./components/searchFormItemsCommon/mixin";
import * as mixins from "@/utils/mixins";
import { checkPermission, generateWorkOrderStatusArr } from "@/utils/common";
import ConfirmBack from "./detailsComponents/ConfirmBack.vue";

export default {
  name: "process",
  mixins: [mixins.affirm, defaultSearchFromForURLQueryMixin],
  components: {
    tablePage,
    searchFormItemsCommon,
    detailsDialog,
    leaveMessageDialog,
    tableColumnsCommon,
    appealDialog,
    suspendDialog,
    postponeDialog,
    completeDialog,
    overDialog,
    ConfirmBack,
  },
  filters: { workOrderStatus, timeRemaining, workOrderPublicStatus },
  data() {
    const statusArr = generateWorkOrderStatusArr(
      [
        { id: "2", name: "待确认" },
        { id: "3", name: "调查中" },
        { id: "7", name: "已中止" },
        { id: "4", name: "待办结" },
        { id: "15", name: "待终止" },
        { id: "16", name: "复核中" },
      ],
      [
        [checkPermission(["允许取消回退"]), { id: "1", name: "回退中" }],
        // [checkPermission(['允许取消确认']), { id: '3', name: '调查中' }],
        [checkPermission(["允许取消提交办结"]), { id: "4", name: "待办结" }],
      ]
    );
    return {
      searchForm: initSearchForm({ status: statusArr[0].id }),
      searchFormRules: initSearchFormRules(),
      currentRow: {},
      detailsDialog: {
        editDetails: {},
        visible: false,
      },
      leaveMessageDialog: { visible: false },
      appealDialog: { visible: false },
      suspendDialog: { visible: false },
      postponeDialog: { visible: false },
      completeDialog: { visible: false },
      overDialog: { visible: false },
      confirmBack: { visible: false },
      statusArr,
      hasDebtAmount: false,
    };
  },
  created() {},
  methods: {
    requestFunc: workOrderApi.process.list,
    rePlayLoadData() {
      this.$refs.tablePage.playLoadData();
      if (this.detailsDialog.visible) {
        this.$refs.detailsDialog.playLoadData();
      }
    },
    handleReadComment() {
      // this.currentRow.appraiseFlag = 'N'
    },
    handleReadLeaveMessage() {
      this.currentRow.wordsFlag = "N";
    },
    handleShowDialog(type, row) {
      this[type].visible = true;
      this.currentRow = row;
    },
    handleStart() {
      this.confirm("确定要开始处理该案件么？")
        .then(() => workOrderApi.process.start(this.currentRow.workOrderNo))
        .then(this.rePlayLoadData);
    },
    handleCancel() {
      this.confirm("确定要取消回退该案件么？")
        .then(() => workOrderApi.process.cancelAppeal(this.currentRow.workOrderNo))
        .then(this.rePlayLoadData);
    },
    handleCancelComplete() {
      this.confirm("确定要取消提交办结该案件么？")
        .then(() => workOrderApi.process.cancelFinishProcess(this.currentRow.workOrderNo))
        .then(this.rePlayLoadData);
    },
    handleContinueProcess() {
      this.confirm("确认继续办理此案件么？")
        .then(() => workOrderApi.process.continueProcess(this.currentRow.workOrderNo))
        .then(this.rePlayLoadData);
    },
    handleCancelStart() {
      this.confirm("确定要取消确认案件么？")
        .then(() => workOrderApi.process.cancelconfirmation(this.currentRow.workOrderNo))
        .then(this.rePlayLoadData);
    },
    onYanQi(details) {
      if (details.addTimeNum >= 3) {
        this.$alert("当前案件延期次数已超过规定的3次，不可申请延期，请在规定时间内完成办理。", "提示");
      } else {
        this.postponeDialog.visible = true;
        this.currentRow = { ...details };
      }
    },
    onCompleteCase(details) {
      this.currentRow = { ...details };
      // 拖欠账款
      this.hasDebtAmount = this.currentRow.questionMainInfo.govArrears.arrearsAmount > 0;
      this.completeDialog.visible = true;
    },
  },
};
</script>

<style scoped>
iframe {
  border: none;
}
</style>
